package com.fz.song.web.controller;

import com.fz.song.framework.common.R;
import com.fz.song.web.entity.User;
import com.fz.song.web.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = "幂等性demo-SelectInsert")
@Slf4j
@RestController
@RequestMapping("idempotent")
public class SelectInsertController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "selectInsert", notes = "在一些并发不高的情况下,使用先selet再insert")
    @GetMapping("selectInsert")
    public R selectInsert(String idcard) {

        User user = userService.getByIdCard(idcard);
        if (user == null) {
            user = new User();
            user.setName("SelectInsert");
            user.setIdCard(idcard);
            user.setVersion(0);
            user.setMoney(1000);
            user.setStock(1000);
            userService.save(user);
            return R.ok("保存成功");
        } else {
            return R.fail(1000, "保存失败");
        }

    }


}
